package com.bree.erp.jsf;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import org.apache.log4j.Logger;

import com.bree.erp.entity.ItemRequest;
import com.bree.erp.entity.PartUnit;
import com.bree.erp.entity.PurchaseRequisition;
import com.bree.erp.entity.Status;
import com.bree.erp.jsf.httpsess.SessionDataKey;
import com.bree.erp.jsf.httpsess.UserSessionData;
import com.bree.erp.jsf.view.PurchaseOrderRequestView;
import com.bree.erp.jsf.vo.ItemRequestDT;
import com.bree.erp.service.PoItemService;
import com.bree.erp.service.PurchaseRequisitionService;

@ManagedBean(name = "purchaseOrderBean")
@RequestScoped
public class PurchaseOrderBean {
	
	private static Logger LOG = Logger.getLogger(PurchaseOrderBean.class);

	@ManagedProperty(value = "#{userSession}")
	private UserSessionData userSession;
	
	@ManagedProperty(value = "#{dtPurchaseOrderRequestView}")
	private PurchaseOrderRequestView purchaseOrderRequestView;

	@EJB
	private PurchaseRequisitionService prService;
	
	@EJB
	private PoItemService piService;

	private PurchaseRequisition purchaseRequisition;
	
	private List<ItemRequestDT> requests;

	@PostConstruct
	public void initPurchaseRequisition() {

		Long id = userSession.get(
				SessionDataKey.SELECTED_PURCHASE_REQUISITION_ID, Long.class);

		if (id != null) {
			purchaseRequisition = prService.read(id, true);
		}
		
		initRequests();
		
	}
	
	private void initRequests() {
		
		LOG.debug("init request list...");

		if(purchaseRequisition != null && purchaseRequisition.getRequests() != null) {
			
			Map<PartUnit, Double> mapQty = piService.totalQuantity(purchaseRequisition.getId());
			
			requests = new ArrayList<ItemRequestDT>();
			
			int count = 0;
			for(ItemRequest item : purchaseRequisition.getRequests()) {
				
				ItemRequestDT dt = new ItemRequestDT(item);
				
				Double ordered = mapQty.get(new PartUnit(dt.getPartUnitId()));
				
				dt.setItemNumber(++count);
				dt.setOrderedQuantity(ordered);
				
				requests.add(dt);
			}
			
			LOG.debug("request list size: " + count);
			
			// init scope view
			purchaseOrderRequestView.initDataModel(requests);
		}
	}

	/**
	 * @return the purchaseRequisition
	 */
	public PurchaseRequisition getPurchaseRequisition() {
		return purchaseRequisition;
	}

	/**
	 * @param purchaseRequisition the purchaseRequisition to set
	 */
	public void setPurchaseRequisition(PurchaseRequisition purchaseRequisition) {
		this.purchaseRequisition = purchaseRequisition;
	}

	/**
	 * @return the userSession
	 */
	public UserSessionData getUserSession() {
		return userSession;
	}

	/**
	 * @param userSession the userSession to set
	 */
	public void setUserSession(UserSessionData userSession) {
		this.userSession = userSession;
	}

	/**
	 * @return the requests
	 */
	public List<ItemRequestDT> getRequests() {
		return requests;
	}

	/**
	 * @return the purchaseOrderRequestView
	 */
	public PurchaseOrderRequestView getPurchaseOrderRequestView() {
		return purchaseOrderRequestView;
	}

	/**
	 * @param purchaseOrderRequestView the purchaseOrderRequestView to set
	 */
	public void setPurchaseOrderRequestView(
			PurchaseOrderRequestView purchaseOrderRequestView) {
		this.purchaseOrderRequestView = purchaseOrderRequestView;
	}
}
